package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Sanmier
 * @create 2020-12-01 14:01
 */
@Api(tags = "品牌数据接口")
@RestController
@RequestMapping("/admin/product/baseTrademark")
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    //分页查询品牌列表
    //    http://api.gmall.com/admin/product/baseTrademark/{page}/{limit}
    @ApiOperation(value = "分页列表")
    @GetMapping("{page}/{limit}")
    public Result getBaseTrademarkList(@PathVariable Long page,
                                @PathVariable Long limit){
        Page<BaseTrademark> objectPage = new Page<>(page, limit);
        IPage baseTrademarkList = baseTrademarkService.getBaseTrademarkList(objectPage);
        return Result.ok(baseTrademarkList);
    }


    //增加品牌
    //http://api.gmall.com/admin/product/baseTrademark/save
//    接受前台传递过来的json字符串  使用@RequestBody  转换为Java对象
    @ApiOperation(value = "新增BaseTrademark")
    @PostMapping("save")
    public Result save(@RequestBody BaseTrademark baseTrademark){
        //此时不需要这么做了！  可以独自新写一个关于baseTrademarkService
        //manageService.save(baseTrademark);
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }


    //修改品牌
    //http://api.gmall.com/admin/product/baseTrademark/update
    @ApiOperation(value = "修改BaseTrademark")
    @PutMapping("update")
    public Result update(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }


    //删除品牌
    //http://api.gmall.com/admin/product/baseTrademark/remove/{id}
    @ApiOperation(value = "删除BaseTrademark")
    @DeleteMapping("remove/{id}")
    public Result delete(@PathVariable Long id){
        baseTrademarkService.removeById(id);
        return Result.ok();
    }


    //根据商品id获取品牌
    //http://api.gmall.com/admin/product/baseTrademark/get/{id}
    @ApiOperation(value = "获取BaseTrademark")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }


    //获取品牌列表
    //http://api.gmall.com/admin/product/baseTrademark/getTrademarkList
    @ApiOperation(value = "获取品牌属性")
    @GetMapping("getTrademarkList")
    public Result getTrademarkList(){
        //select * from base_base_trademark;
        return Result.ok(baseTrademarkService.list(null));
    }
}
